<template>
  <div id="home">
    <nav-bar class="home-nav">
      <template v-slot:center>
        购物街
      </template>
    </nav-bar>
    <tab-control :titles="['流行','新款','精选']"
                 class="tab-control"
                 @tabClick="tabClick"
                 ref="tabControl1"  v-show="isTabFixed"></tab-control>
    <scroll class="content"
            ref="scroll"
            :probe-type="3"
            @scroll="contentScroll"
            :pull-up-load="true" @pullingUp="loadMore">
      <home-swiper :banners="banners"></home-swiper>
      <recommend-view :recommends="recommends"></recommend-view>
      <feature-view></feature-view>
      <tab-control :titles="['流行','新款','精选']"
                   @tabClick="tabClick"
                   ref="tabControl"></tab-control>
      <goods-list :goods="goods[currentType].list"></goods-list>
    </scroll>

    <back-top @click.native="backClick" v-show="isShowBackTop"></back-top>
  </div>
</template>

<script>
  //页面子组件
  import NavBar from "@/components/common/navbar/NavBar";
  import HomeSwiper from "@/views/home/childComps/HomeSwiper";
  import RecommendView from "@/views/home/childComps/RecommendView";
  import FeatureView from "@/views/home/childComps/FeatureView";
  import TabControl from "@/components/content/tabControl/TabControl";
  import GoodsList from "@/components/content/goods/GoodsList";

  //网络请求方法
  import {getHomeMultidata,
    getHomeGoods}
    from "@/network/home";

  import scroll from "@/components/common/scroll/Scroll";
  import BackTop from "@/components/content/backTop/BackTop";



  export default {
    name: "Home",
    components: {
      NavBar, //头部导航栏组件
      HomeSwiper, //轮播图
      RecommendView, //推荐
      FeatureView,//本周推荐
      TabControl, //选项卡
      GoodsList, //商品列表页
      scroll, //滚动
      BackTop
    },
    data() {
      return {
        banners: [],//轮播图数据
        recommends: [], //推荐数据
        //商品数据
        goods: {
          'pop' : {page: 0,list: []},
          'new' : {page: 0,list: []},
          'sell' : {page: 0,list: []}
        },
        /** 当前类型*/
        currentType: 'pop',
        /**是否显示BackTop*/
        isShowBackTop: true,
        tabOffsetTop: 0,
        isTabFixed: false
      }
    },
    computed: {
      /** @see this.goods 商品数据
       * @see this.currentType 选项卡现在所处的类型
       * @see 展示的商品数组
       * @returns []
       **/
      showGoods() {
        return this.goods[this.currentType].list
      }
    },
    methods: {
      // 事件监听相关方法
      // 选项卡点击
      tabClick(index) {
        switch (index) {
          case 0:
            this.currentType = 'pop'
            break
          case 1:
            this.currentType = 'new'
            break
          case 2:
            this.currentType = 'sell'
            break
        }
        //实现两个tabControl的同步
        this.$refs.tabControl1.currentIndex = index
        this.$refs.tabControl.currentIndex = index
      },

      backClick() {
        this.$refs.scroll.scrollTo(0,0,500)
      },

      contentScroll(position) {
        //1. 判断BackTop是否显示
        this.isShowBackTop = (-position.y) > 1000

        //2. 决定tabControl是否吸顶(position:fixed)
        this.isTabFixed = (-position.y) >this.tabOffsetTop
      },

      /**
       * 上拉加载更多事件方法
       * */
      loadMore() {
        this.getHomeGoods(this.currentType)
        this.$refs.scroll.refresh()
      },


      /**
       * 网络请求相关方法
       */

      //1.请求多个数据
      getHomeMultidata() {
        getHomeMultidata().then(res => {
          this.banners = res.data.banner.list
          this.recommends = res.data.recommend.list
        })
      },
      //2.请求商品数据
      getHomeGoods(type) {
        const page = this.goods[type].page+1
        getHomeGoods(type,page)
            .then(res => {
              //将获取元素放入到要展示的数据中
              this.goods[type].list.push(...res.data.list)
              //页面+1
              this.goods[type].page += 1
              console.log(this.goods[type].list);

              //为了能进行下一次加载更多
              this.$refs.scroll.finishPullUp()
            })
      }
    },
    created() {
      //1.请求多个数据
      this.getHomeMultidata()

      //2.请求多个数据
      this.getHomeGoods('pop',0)
      this.getHomeGoods('new',0)
      this.getHomeGoods('sell',0)
    },
    mounted() {
      setTimeout(() => {
        this.tabOffsetTop = this.$refs.tabControl.$el.offsetTop
      },500)
    }
  }
</script>

<style scoped>
  /*#home {*/
  /*  padding-top: 44px;*/
  /*}*/

  .home-nav {
    /*固定定位*/
    /*position: fixed;*/
    /*left: 0;*/
    /*right: 0;*/
    /*top: 0;*/
    /*z-index: 9;*/

    /*背景色*/
    background-color: var(--color-tint);
    /*文字颜色*/
    color: #ffffff;
  }

  /*.tab-control {*/
  /*  !*粘性定位,在一定条件下固定 相对定位与固定定位的结合*!*/
  /*  position: sticky;*/
  /*  !*粘性定位必须属性*!*/
  /*  top: 44px;*/
  /*  z-index: 9;*/
  /*}*/

  .content {
    height: calc(100vh - 93px);
    overflow: hidden;
  }

  .tab-control {
    position: relative;
    z-index: 9;
  }

</style>
